package com.ithinks.flow.common.utils.protocol.socket.aio.server;


import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

/**
 * @Description
 * @ClassName AioAcceptHandler
 * @Author MR.LI
 * @date 2020.12.07 18:13
 */
public class AioAcceptHandler
        implements CompletionHandler<AsynchronousSocketChannel,
        AioServerHandler> {
    @Override
    public void completed(AsynchronousSocketChannel channel, AioServerHandler serverHandler) {
        AioServer.clientCount++;
        System.out.println("连接得客户端数："+AioServer.clientCount);
        serverHandler.channel.accept(serverHandler,this);
        ByteBuffer readBuffer = ByteBuffer.allocate(1024);
        channel.read(readBuffer,readBuffer,new AioReadHandler(channel));
    }

    @Override
    public void failed(Throwable exc, AioServerHandler serverHandler) {

        exc.printStackTrace();
        serverHandler.latch.countDown();
    }
}